﻿using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
	public float speed = 10.0f;
	public float turnspeed = 0.02f;
	public float movement_radius=50.0f;
	
	private float moveDirection=0.0f;
	private float turnDirection=0.0f;
	
	GameObject player;
	public Transform target;
	Vector3 startpt;
	Vector3 endpt;
	
	RaycastHit hit;
	LineRenderer rend;
	
	
	
	// Use this for initialization
	void Start () 
	{
		player=GameObject.Find ("PFB_MainShip");
		startpt=target.position;
		hit=new RaycastHit();
		rend=player.AddComponent<LineRenderer>();
		rend.SetWidth (0.001f,0.001f);
		rend.SetPosition(0,target.position);
	
	}
	
	void Update()
	{
			Vector3 test;
			test.x=Input.mousePosition.x;
			test.y=Input.mousePosition.y;
			test.z=0.5f;
			endpt = Camera.main.ScreenToWorldPoint(test);
		//endpt.y=1f;
		Debug.LogWarning (target.position);
			Debug.LogWarning (endpt);
			rend.SetPosition(1,endpt);
		
	}
	
	// Update is called once per frame (FixedUpdate provides stability for off-sync functionality)
	void FixedUpdate () 
	{
		//Clicking Movement
		
		
		if (Input.GetMouseButtonDown(0))
		{
			Vector3 test;
			test.x=Input.mousePosition.x;
			test.y=Input.mousePosition.y;
			test.z=0.5f;
			endpt = Camera.main.ScreenToWorldPoint(test);
			rend.SetPosition(1,endpt);
			
			//endpt=Input.mousePosition;

			//transform.position=Vector3.Lerp (startpt,endpt,(Time.deltaTime));
			//rigidbody.AddRelativeForce (0,0,moveDirection);
			//rigidbody.MovePosition (endpt);
			//rigidbody.MovePosition (rigidbody.position + endpt * Time.deltaTime);
			//transform.position=Vector3.Lerp (startpt,endpt,(Time.deltaTime));
			//transform.position = Vector3.MoveTowards(target.position, endpt, Time.deltaTime * speed);
			endpt.z=0.5f;

			Debug.LogWarning (target.position.x+","+target.position.y+","+target.position.z+"--"+endpt.x+","+endpt.y+","+endpt.z+"");
			//player.transform.position=endpt;
			  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(ray, out hit, 1000.0f)) 
			{

                Vector3 newpos = new Vector3(hit.point.x, hit.point.y, 0);

                player.transform.position = newpos;
				rend.SetPosition(0,target.position);

                

                // collect the direction

                Vector3 dir = (newpos - player.transform.position).normalized;

                // translate at speed

                //player.transform.Translate(dir * speed * Time.deltaTime, Space.World);
			}
			
		}


		/*
		//Torque and Angular Velocity Tweaks (
		float currentSpeed=Mathf.Abs (transform.InverseTransformDirection (rigidbody.velocity).z);
		
		//Linear Interpolation - Current,Max,and Time. The lower the drag, the faster the ship can move. As Vel inc, more drag
		float maxAngularDrag=1.0f;
		float currentAngularDrag=2.5f;
		//Multiply by 0.1 because range is from 0-1
		float dragLerpTime=currentSpeed*0.1f;
		
		float myAngularDrag=Mathf.Lerp (currentAngularDrag,maxAngularDrag,dragLerpTime);
		float myDrag=Mathf.Lerp (currentAngularDrag,maxAngularDrag,dragLerpTime);		
		
	
		//Rigid Body Functions - Init (Physics Movement)
		//WASD Movement
		moveDirection=Input.GetAxis ("Vertical") * speed;
		//Whatever value of moveDirection is the direction its propelled by force
		rigidbody.AddRelativeForce (0,0,moveDirection);
		rend.SetPosition(0,target.position);
		
		turnDirection=Input.GetAxis ("Horizontal") * turnspeed;
		//Whatever value of moveDirection is the direction its propelled by force
		rigidbody.AddRelativeTorque (0,turnDirection,0);
		

		
		rigidbody.angularDrag=myAngularDrag;
		rigidbody.drag=myDrag;
		
		*/
		
		
	}
}